/********************************************************************
filename : wolf_iic_MMA7455L.c
discript : dreamwolf on board Accelerometer(MMA7455L) control driver,
					 device address is:0b0011101x,pin connect as below:
					 IIC_SDA  -> PB7,
					 IIC_CLK  -> PB6,
					 CS       -> PF8,
					 DRDY     -> PF9,
					 ADDR0    -> GND.
version  : V0.0
editor   : Icy - dreamwolf
time     : 2014.3.2
statement: This file is modified by dreamwolf under LGPL,you could use 
           it free.We cann't ensure there is no error in this file,and
				   if you detect an error please contact us freely,it is so a-
					 ppreciate for your help to improve our code that could help 
					 more people to use it safty.
contact  : edreamtek@163.com
           www.edreamtek.com
********************************************************************/

#include "wolf_iic_MMA7455L.h"

void wolf_iic_MMA7455_CS_init(void);
int16_t wolf_complement_true_form(uint16_t cpt_dat,uint8_t length);




uint8_t MMA7455_reg[MMA7455_REG_CNT]; 

MMA7455_CPT_DATA MMA7455_cpt_dat;
MMA7455_DATA MMA7455_dat;



void wolf_iic_MMA7455_init(void)
{
	wolf_iic_MMA7455_CS_init();
	MMA7455_CS_HIGH();
	wolf_iic1_init();	
}

void wolf_iic_MMA7455_read_data(uint8_t dat_length)
{
	uint8_t *pdat;
	if(dat_length == DAT_8_BITS)
	{
		pdat = MMA7455_reg;
		wolf_iic_MMA7455_read_bytes(XOUT8, pdat+XOUT8,3);
		MMA7455_cpt_dat.X_dat = MMA7455_reg[XOUT8];
		MMA7455_cpt_dat.Y_dat = MMA7455_reg[YOUT8];
		MMA7455_cpt_dat.Z_dat = MMA7455_reg[ZOUT8];
	}
	else if(dat_length == DAT_10_BITS)
	{
		pdat = MMA7455_reg;
		wolf_iic_MMA7455_read_bytes(XOUTL, pdat+XOUTL,6);
		MMA7455_cpt_dat.X_dat = (((uint16_t)MMA7455_reg[XOUTH])<<8)+MMA7455_reg[XOUTL];
		MMA7455_cpt_dat.Y_dat = (((uint16_t)MMA7455_reg[YOUTH])<<8)+MMA7455_reg[YOUTL];
		MMA7455_cpt_dat.Z_dat = (((uint16_t)MMA7455_reg[ZOUTH])<<8)+MMA7455_reg[ZOUTL];
	}
}

void wolf_iic_MMA7455_true_form_data(uint8_t length)
{
	MMA7455_dat.X_dat =  wolf_complement_true_form(MMA7455_cpt_dat.X_dat,length);
	MMA7455_dat.Y_dat =  wolf_complement_true_form(MMA7455_cpt_dat.Y_dat,length);
	MMA7455_dat.Z_dat =  wolf_complement_true_form(MMA7455_cpt_dat.Z_dat,length);
}

void wolf_iic_MMA7455_CS_init(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure;
  
  /* Enable the GPIO_CS Clock */
  RCC_AHBPeriphClockCmd(MMA7455_CS_GPIO_CLK, ENABLE);


  /* Configure the GPIO_CS pin */
  GPIO_InitStructure.GPIO_Pin = MMA7455_CS_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  GPIO_Init(MMA7455_CS_GPIO_PORT, &GPIO_InitStructure);
}

void wolf_iic_MMA7455_write_byte(uint16_t addr,uint8_t dat)
{
	wolf_iic1_write_byte(MMA7455_HW_ADDRESS, ADDR_8_BITS, addr, dat);
}

uint8_t wolf_iic_MMA7455_read_byte(uint32_t addr)
{
	return wolf_iic1_read_byte(MMA7455_HW_ADDRESS, ADDR_8_BITS, addr);
}

void wolf_iic_MMA7455_write_bytes(uint16_t addr,uint8_t* pdat,uint16_t cnt)
{
	wolf_iic_write_bytes(MMA7455_HW_ADDRESS, ADDR_8_BITS, addr, pdat, cnt);
}

void wolf_iic_MMA7455_read_bytes(uint32_t addr,uint8_t* pdat,uint16_t cnt)
{
	wolf_iic_read_bytes(MMA7455_HW_ADDRESS, ADDR_8_BITS, addr, pdat, cnt);
}

void wolf_iic_MMA7455_read_register(void)
{
	uint8_t *pdat;
	pdat = MMA7455_reg;
	wolf_iic_MMA7455_read_bytes(0x00, pdat,MMA7455_REG_CNT);
}

int16_t wolf_complement_true_form(uint16_t cpt_dat,uint8_t length)
{
	int16_t dat;
	
	if(cpt_dat & (0x1<<(length-1)))		/*data<0*/
	{
		cpt_dat &= ~(0x1<<(length-1));
		cpt_dat = ~cpt_dat +1;
		if(length == DAT_8_BITS)
		{
			cpt_dat &= DAT_8_MASK;
		}else if(length == DAT_10_BITS)
		{
			cpt_dat &= DAT_10_MASK;		
		}
		dat = -cpt_dat;		
	}else
	{												/*data>=0*/
		dat = cpt_dat;
	}
	
	return dat;
}
